<?php


function decrypt ($decrypt){
	$key = C('DES_KEY');

    // 不需要設定 IV
    $str = mcrypt_decrypt(MCRYPT_DES, $key, base64_decode($decrypt), MCRYPT_MODE_ECB);

    // 根據 PKCS#7 RFC 5652 Cryptographic Message Syntax (CMS) 修正 Message 移除 Padding
    $pad = ord($str[strlen($str) - 1]);
    return substr($str, 0, strlen($str) - $pad);
}

function encrypt ($encrypt){
	$key = C('DES_KEY');

    // 根據 PKCS#7 RFC 5652 Cryptographic Message Syntax (CMS) 修正 Message 加入 Padding
    $block = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_ECB);
    $pad = $block - (strlen($encrypt) % $block);
    $encrypt .= str_repeat(chr($pad), $pad);

    // 不需要設定 IV 進行加密
    $passcrypt = mcrypt_encrypt(MCRYPT_DES, $key, $encrypt, MCRYPT_MODE_ECB);
    return base64_encode($passcrypt);
}


function getinfo($id,$table,$field)
{
	$parent = M($table);
	if (empty($id))
	{
		return '';
	}
	$info = $parent ->find($id);
	return $info[$field];
	unset($parent);
}

function loadinfo($id,$type)
{
	$map['user_id']= $id;
	if ($type == 1)
    {
    	$mteacher = M('teacher');
        $info = $mteacher ->where($map)->find();
    }
    else if ($type == 2)
    {
    	$mparent = M('parent');
        $info = $mparent ->where($map)->find();
    }
    return $info;
    unset($mteacher);
    unset($mparent);
	unset($info);
}

function getuserinfo($id,$type,$field)
{
	if (empty($id))
	{
		return '';
	}
	$m=M($type);
	$info = $m ->find($id);
	$user_id =$info['user_id'];
	$muser =M('user');
	$uinfo =$muser -> find($user_id);
	return $uinfo[$field];
}
function loadcondles($id,$day,$lesson_type,$subject_id='',$week='',$teacher='',$teacher_id)
{
	$mlesson=M('lesson');
	if (!empty($week))
	{
		$wstr .=' and week_day='.$week;
	}
	if (!empty($teacher))
	{
		$wstr .=" and teacher.XM like '%".$teacher."%'";
	}
	if (!empty($teacher_id))
	{
		$wstr .=" and teacher_id=".$teacher_id;
	}

	$lessoninfo = $mlesson->field('lesson.id,lesson.subject_id,teacher_id')->where('schedule.XXZT=1 and week_day='.$day.' and lesson_type_id ='.$lesson_type.' and schedule.class_id ='.$id.$wstr)->join('schedule on schedule.id =lesson.schedule_id')->join('teacher on teacher.id =lesson.teacher_id')->find();
	if (empty($lessoninfo))
	{
		return "";
	}
	else
	{
		$subject= M('subject');
		if (!empty($subject_id))
		{
			if ($lessoninfo['subject_id']!=$subject_id)
			{
				return '';
			}
			else
			{
				$subjectinfo = $subject ->find($lessoninfo['subject_id']);
				$teacherinfo = loadinfo($lessoninfo['teacher_id'],1);
				return '<p class="txt_less">'.$subjectinfo['KCMC'].'</p>
								<span class="txt_teach">'.getinfo($lessoninfo['teacher_id'],'teacher','XM').'</span>';
			}
		}
		else
		{
			$subjectinfo = $subject ->find($lessoninfo['subject_id']);
			$teacherinfo = loadinfo($lessoninfo['teacher_id'],1);
			return '<p class="txt_less">'.$subjectinfo['KCMC'].'</p>
								<span class="txt_teach">'.getinfo($lessoninfo['teacher_id'],'teacher','XM').'</span>';
		}


	}

}

function loadclsles($id,$day,$lesson_type,$type = '')
{
	$mlesson=M('lesson');

	$lessoninfo = $mlesson->field('lesson.id,subject_id,teacher_id')->where('schedule.XXZT=1 and week_day='.$day.' and lesson_type_id ='.$lesson_type.' and schedule.class_id ='.$id)->join('schedule on schedule.id =lesson.schedule_id')->find();
	if (empty($lessoninfo))
	{
		return "";
	}
	else
	{
		$subject= M('subject');
		$subjectinfo = $subject ->find($lessoninfo['subject_id']);



		if (!$type)
		{
			return $subjectinfo['KCMC'].'<br>'.getinfo($lessoninfo['teacher_id'],'teacher','XM').'<br/ ><a onClick="'."chgtoedit('kc_".$day."_".$lesson_type."',".$lessoninfo['id'].")".'">修改</a>';
		}
		else
		{
			return '<p class="txt_less">'.$subjectinfo['KCMC'].'</p>
								<span class="txt_teach">'.getinfo($lessoninfo['teacher_id'],'teacher','XM').'</span>';
		}
	}

}
function loadattence($num,$time,$stuid,$class_id,$week)
{

	$mstudent_attence = M('student_attence');
	 $list = $mstudent_attence->where('student_attence.student_id ='.$stuid.' and schedule.XXZT=1 and student_attence.FBSJ='.$time.' and lesson.num='.$num.' and week_day='.$week)->join("lesson on lesson.id =student_attence.lesson_id")->join('schedule on schedule.id =lesson.schedule_id')->find();
	if ($list['status']==1)
	{
		return '已到';
	}
	else if ($list['status']==0)

	{		return "未到";
	}

}

function randchar($t='')
{
	if ($t!='')
	{
    	$chars = '0123456789';
    }
    else
    {
    	$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    }
    for ($i = 0; $i < 8; $i++)
    {
        $str .= $chars[ mt_rand(0, strlen($chars) - 1) ];
    }

    return $str;
}
function loadlesson($id,$day,$lesson)
{
	$mlesson=M('lesson');
	$lessoninfo = $mlesson->field('subject_id')->where('schedule.XXZT=1 and week_day='.$day.' and num ='.$lesson.' and lesson.teacher_id ='.$id)->join('schedule on schedule.id =lesson.schedule_id')->find();
	if (empty($lessoninfo))
	{
		return "";
	}
	else
	{
		$subject= M('subject');
		$subjectinfo = $subject ->find($lessoninfo['subject_id']);
		$teacherinfo = loadinfo($id,1);
		return $subjectinfo['KCMC'].'<br>'.getinfo($id,'teacher','XM');
	}
}
function levellist($id='')
{
	$school_id=$_SESSION['school_id'];
	$school_type_id = getinfo($school_id,'school','school_type_id');
    $map['school_type_id'] = $school_type_id;
    $mlevel =M('level');
    $levellist =$mlevel->where($map)->select();
    echo '<select id="level_id" name="level_id" class="ui-select">
									<option value="">
									请选择
									</option>';
	foreach ($levellist as $vcl) {
		if ($id == $vcl['id'])
		{
			echo '<option value="'.$vcl['id'].'"  selected="selected">
									'.$vcl['level_name'].'
									</option>';
		}
		else
		{
		# code...
			echo '<option value="'.$vcl['id'].'">
									'.$vcl['level_name'].'
									</option>';
		}
	}
	echo '</select>';

}
function classlist($id='',$classlist='')
{
//	echo $id;
	$map['school_id']=$_SESSION['school_id'];
    $mclass =M('class');
    if ($classlist!='')
    {
    	$map['id'] = array('in',$classlist);
    }
    $classlist =$mclass->where($map)->select();
    echo '<select id="class_id" name="class_id" class="ui-select">
									<option value="">
									请选择
									</option>';
	foreach ($classlist as $vcl) {
		if ($id == $vcl['id'])
		{
			echo '<option value="'.$vcl['id'].'"  selected="selected">
									'.$vcl['class_name'].'
									</option>';
		}
		else
		{
		# code...
			echo '<option value="'.$vcl['id'].'">
									'.$vcl['class_name'].'
									</option>';
		}
	}
	echo '</select>';
}

function loadweek($time)
{
	if (!empty($time))
        {
            $timeint =strtotime($_REQUEST['time']);
            $week = date("w",$timeint);
            if ($week==0)
            {
                $week =7;
            }
        }
    return $week;
}
function loadpower($type,$is_goto=0){
		$mclass=M('class');
		$mteacher_power =M('teacher_power');
		
		if($is_goto==1){ //判断是否为学校管理员
			$map['teacher_id']=$_SESSION['schooler_id'];
		}else{
			$map['teacher_id']=$_SESSION['teacher_id'];
		}

		if ($type==1)
		{
			$classlist = $mclass->where($map)->select();
			foreach ($classlist as $cl) {
				$idlist .= $cl['id'].',';
			}

		}
		else if ($type==2)
		{
			$powerlist = $mteacher_power->where($map)->select();
			foreach ($powerlist as $pl) {
				$idlist .= $pl['class_id'].',';
			}
		}
		else if ($type==3)
		{
			$classlist = $mclass->where($map)->select();
			foreach ($classlist as $cl) {
				$idlist .= $cl['id'].',';
			}
			$powerlist = $mteacher_power->where($map)->select();
			foreach ($powerlist as $pl) {
				$idlist .= $pl['class_id'].',';
			}
		}

		$idlist .= '0';
		return  $idlist;

	}
	function loadscore($exam_id,$student_id,$t)
	{
		$mstudent_result =M('student_result');
		$map['exam_id'] =$exam_id;
		$map['student_id'] =$student_id;


		if ($t==1)
		{
			$resultlist =$mstudent_result->where($map)->select();
			foreach ($resultlist as $vl) {
				$result .= getinfo($vl['subject_id'],'subject','KCMC').'：'.$vl['FSLKSCJ'].'<br />';
				# code...
			}
		}
		if ($t==2)
		{
			$result =$mstudent_result->where($map)->sum('FSLKSCJ');
		}
		if ($t==3)
		{
			$total = $mstudent_result->where($map)->sum('FSLKSCJ');
			$num = 	$mstudent_result->where($map)->count();
			$result =$total/$num;
		}
		if ($t==5)
		{
			$mexam =M('exam');
			$list = $mstudent_result->field('student_id')->group('student_id')->select();
			$i=1;
			foreach ($list as $ls) {

				$arr[$i]['total'] = $mstudent_result->where('exam_id='.$exam_id.' and student_id='.$ls['student_id'])->sum('FSLKSCJ');
				$arr[$i]['student_id'] = $ls['student_id'];
				$i++;
			}
			$j=1;
			$arr = array_sort($arr,'total','desc');

			foreach ($arr as $lt) {

				if ($lt['student_id'] == $student_id)
				{
					$result = $j;
					break;
				}
				$j++;
			}
		}
		if ($t==4)
		{
			$mexam =M('exam');
			$class_id =getinfo($student_id,'student','class_id');
			//$stlist = $mstudent_result->field('student_id')->group('student_id')->select();
			$sql = 'select id as student_id from student where id in (select student_id from student_result where exam_id ='.$exam_id.' ) and class_id ='.$class_id;
			$mq =M('');
			$list =$mq->query($sql);
			$i=1;
			foreach ($list as $ls) {

				$arr[$i]['total'] = $mstudent_result->where('exam_id='.$exam_id.' and student_id='.$ls['student_id'])->sum('FSLKSCJ');
				$arr[$i]['student_id'] = $ls['student_id'];
				$i++;
			}
			$j=1;
			$arr = array_sort($arr,'total','desc');

			foreach ($arr as $lt) {

				if ($lt['student_id'] == $student_id)
				{
					$result = $j;
					break;
				}
				$j++;
			}
		}
		echo $result;
	}

	function array_sort($arr,$keys,$type='asc'){
	$keysvalue = $new_array = array();
	foreach ($arr as $k=>$v){
		$keysvalue[$k] = $v[$keys];
	}
	if($type == 'asc'){
		asort($keysvalue);
	}else{
		arsort($keysvalue);
	}
	reset($keysvalue);
	foreach ($keysvalue as $k=>$v){
		$new_array[$k] = $arr[$k];
	}
	return $new_array;
	}

	function loadatt($lesson_id,$student_id,$time)
	{
		$data['FBSJ'] = $time;
    	$data['student_id'] = $student_id;
    	$data['lesson_id'] = $lesson_id;
    	$mstudent_attence=M('student_attence');
    	$count = $mstudent_attence->where($data)->find();
    	return $count['status'];

	}

	function getschool($id,$key='XXMC'){
		$m = M('school');
		$list = $m->find($id);
		return $list[$key];
	}


	function getteacher($id,$key='XM'){
		$m = M('teacher');
		$list = $m->find($id);
		return $list[$key];
	}


	function getlevel($level_id){
		$m = M('level');
		$info = $m->find($level_id);
		return $info['level_name'];
	}

	function getlesson($classid){
		$schedule = M('schedule');
		$sch['class_id'] = $classid;
		$sch['XXZT'] = 1;
		$schlist = $schedule->where($sch)->find();

		if($schlist != null){
			$lesson = M('lesson');
			$map['schedule_id'] = $schlist['id'];
			$lessonlist = $lesson->where($map)->select();
			$str='';
			foreach($lessonlist as $k=>$l){
				$str.='<div style="width:80px; float:left">'.getinfo($l['subject_id'],'subject','KCMC').'('.getteacher($l['teacher_id']).')，</div>';
			}
			return $str;
		}else{
			return "";
		}
	}


	function getclass_student($classid){
		$data['class_id'] = $classid;
		$data['school_id'] = $_SESSION['school_id'];
		$m = M('student');
		$list = $m->where($data)->order('XH asc')->select();
		$str='';
		foreach($list as $k=>$l){
			$str.='<div style="width:100px; float:left">'.$l['XH'].':'.$l['XM'].', </div>';
		}
		return $str;
	}

	function loadscoreresult($exid,$student_id,$subject_id)
	{
		$mstudent_result= M('student_result');
		$map['exam_id']=$exid;
		$map['student_id']=$student_id;
		$map['subject_id']=$subject_id;
		$result = $mstudent_result->where($map)->find();
		echo  $result['FSLKSCJ'];
	}

	function getschool_type($id){
		$m  = M('school_type');
		$list = $m->find($id);
		return $list['type_name'];
	}

	function getclassname($classid){
		$m = M('class');
		$list = $m->find($classid);
		return $list['class_name'];
	}

	function getstudentname($studentid){
		$m = M('student');
		$list = $m->find($studentid);
		return $list['XM'];
	}

	function getsubjectname($id){
		$m = M('subject');
		$list = $m->find($id);
		return $list['KCMC'];
	}



	function getteachersubject($str){
		$m = M('subject');
		$arr = explode(',',$str);
		$data['id'] = array('in',$arr);
		$list = $m->where($data)->select();
		$string="";
		foreach($list as $k=>$l){
			$string.=$l['KCMC'].',';
		}
		return $string;
	}


	function getusername($user_id){
		$m = M('user');
		$list = $m->find($user_id);
		return $list['username'];
	}


	function getworkstatus($iswork){
		if($iswork == 1){
			return "在职";
		}else if($iswork == 0){
			return "离职";
		}
	}
	function countclass($id)
	{
		$map['class_id']=$id;
		$mstudent=M('student');
		$num = $mstudent->where($map)->count();
		return $num;
	}

	function add_log($adminid,$content,$type){
		$m = M('admin_log');
		$data['admin_id'] = $adminid;
		$data['action_content'] = $content;
		$data['addtime'] = time();
		$data['type'] = $type;
		$data['from'] =2;
		$data['fromip'] = $_SERVER['REMOTE_ADDR'];
		$m->add($data);
	}

	function outlog($logid){
		$m = M('admin_log');
		$data['id'] = $logid;
		$data['logouttime'] = time();
		$m->save($data);
	}

function loadhour($time)
{
	$time = explode(":", $time);
	return '<option value="'.$time[0].'">'.$time[0].'</option>';
}
function loadmin($time)
{
	$time = explode(":", $time);
	return $time[1];
	return '<option value="'.$time[1].'">'.$time[1].'</option>';
}

function loadoneproc($id)
{

	$mclass_proclamation=M('class_proclamation');
	$proclist = $mclass_proclamation->where('class_id='.$id)->order('id desc')->limit('0,1')->find();
	return msubstr($proclist['content'],0,20);
}

function loadoneremark($id)
{
	$mstudent_remark=M('student_remark');
	$remarklist = $mstudent_remark->where('student_id='.$id)->order('id desc')->limit('0,1')->find();
	return msubstr($remarklist['JXPY'],0,20);
}
function loadonework($id)
{
	$mstudent_work=M('student_work');
	$worklist = $mstudent_work->where('class_id='.$id)->order('id desc')->limit('0,1')->find();
	return msubstr($worklist['content'],0,20);

}
function loadsubject($id)
{
	$subject= getinfo($id,'teacher','subject_id');

	$mapp['id'] = array('in',$subject);

	$msubject =M('subject');
	$subjectlist =$msubject->where($mapp)->select();
	foreach ($subjectlist as $sjl) {
		$str .= $sjl['KCMC'].'、';
		# code...
	}
	$str =rtrim($str,'、');
	return $str.'老师';

}

function getusersjhm($userid){
	$user = M('user');
	$list = $user->find($userid);
	return $list['SJHM'];
}
function getrelation($sid,$pid)
{
	$mppower = M('parent_power');
	$map['student_id'] =$sid;
	$map['parent_id'] =$pid;
	
	$info = $mppower->where($map)->limit('0,1')->select();
	return $info[0]['relation'];
}
function loadparent($id,$t)
{
	$mppower = M('parent_power');
	$maps['student_id'] =$id;
	$pplist =$mppower->where($maps)->select();
	foreach ($pplist as $pl) {
		 $idlist .= $pl['parent_id'].',';
		# code...
	}
	$idlist =rtrim($idlist,',');
	$map['id'] = array('in',$idlist);
	$mparent=M('parent');
	$parentlist =$mparent->where($map)->select();
	foreach ($parentlist as $plt) {
		switch ($t) {
			case '1':
				$info .= $plt['XM'].'<br>';
				# code...
				break;
			case '2':
				$info .= getrelation($id,$plt['id']).'<br>';
				//$info .= $plt['relation'].'<br>';
				# code...
				break;
			case '3':
				$info .= $plt['LXDH'].'<br>';
				# code...
				break;
			case '4':
				$info .= getinfo($plt['user_id'],'user','SJHM').'<br>';
				# code...
				break;
			default:
				# code...
				break;
		}
				# code...
	}
	return $info;

}

function loadstudentinfo($id,$t){
	$mstudent = M('student');
	$mpower =M('parent_power');
	$map['parent_id']= $id;
	$powerlist = $mpower->where($map)->select();
	foreach ($powerlist as $plt) {
		$studentid .= $plt['student_id'];

		# code...
	}
	$studentid =rtrim($studentid,',');
	$map_parent['id'] = array('in',$studentid);


	$studentlist = $mstudent->where($map_parent)->select();
	foreach ($studentlist as $stl) {
		switch ($t) {
			case '1':
				$info .=$stl['XM'].'<br/>';
				# code...
				break;
			case '2':
				$info .= getinfo($stl['class_id'],'class','class_name').'<br/>';
				# code...
				break;
			
		}
		# code...
	}
	return $info;

	
}

?>